#include "DataFilterDialog.h"
#include "ui_datafilterdialog.h"
#include <QStandardItemModel>
#include <QDebug>
DataFilterDialog::DataFilterDialog(QStringList dataSourceList,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DataFilterDialog)
{
    ui->setupUi(this);
    this->myDataSourceList = dataSourceList;
    QStandardItemModel *student_model = new QStandardItemModel();
    //利用setModel()方法将数据模型与QTableView绑定
    for(int i=0; i<dataSourceList.length(); i++){
        QString aa = dataSourceList[i];
        student_model->setItem(i, 0, new QStandardItem(aa));
    }
    ui->tableView->setModel(student_model);

    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView->setSelectionMode(QAbstractItemView::MultiSelection);
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);

    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->tableView->horizontalHeader()->setMinimumSectionSize(100);

    ui->tableView_2->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView_2->setSelectionMode(QAbstractItemView::MultiSelection);
    ui->tableView_2->setEditTriggers(QAbstractItemView::NoEditTriggers);

    ui->tableView_2->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->tableView_2->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->tableView_2->horizontalHeader()->setMinimumSectionSize(100);
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(addPushAction()));
    connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(delPushAction()));

}

DataFilterDialog::~DataFilterDialog()
{
    delete ui;
}

void DataFilterDialog::addPushAction()
{
    QStandardItemModel *studentModel = qobject_cast< QStandardItemModel *>(ui->tableView->model());
    QItemSelectionModel *selections =  ui->tableView->selectionModel();
    QModelIndexList selected = selections->selectedIndexes();
    if(selected.count()==0){
        return;
    }

    QStandardItemModel *teacherModel = new QStandardItemModel();
    foreach (QModelIndex index, selected)
    {
        QStandardItem *item =  studentModel->itemFromIndex(index);
        resultSourceList<<item->text();
    }

    for(int i=0; i<resultSourceList.length(); i++){
        QString aa = resultSourceList[i];
        teacherModel->setItem(i, 0, new QStandardItem(aa));
    }
    ui->tableView_2->setModel(teacherModel) ;

    QStandardItemModel *studentModel2 = new QStandardItemModel();
    for(int i=0; i<this->myDataSourceList.length(); i++){
        QString aa = this->myDataSourceList[i];
        bool isExist = true;
        for(int j=0; j<resultSourceList.length(); j++){
            QString bb = resultSourceList[j];
            if(aa == bb){
                isExist = false;
            }
        }
        if(isExist)
            studentModel2->setItem(i, 0, new QStandardItem(aa));
    }
    qDebug()<<studentModel2<<endl;

    ui->tableView->setModel(studentModel2) ;

}

void DataFilterDialog::delPushAction()
{

    QStandardItemModel *teacherModel = qobject_cast< QStandardItemModel *>(ui->tableView_2->model());
    QItemSelectionModel *selections =  ui->tableView_2->selectionModel();
    QModelIndexList selected = selections->selectedIndexes();
    if(selected.count()==0){
        return;
    }

    foreach (QModelIndex index, selected)
    {
        QStandardItem *item =  teacherModel->itemFromIndex(index);
        resultSourceList.removeOne(item->text());
    }

    QStandardItemModel *teacherModel2 = new QStandardItemModel();
    for(int i=0; i<resultSourceList.length(); i++){
        QString aa = resultSourceList[i];
        teacherModel2->setItem(i, 0, new QStandardItem(aa));
    }
    ui->tableView_2->setModel(teacherModel2) ;

    QStandardItemModel *studentModel2 = new QStandardItemModel();
    for(int i=0; i<this->myDataSourceList.length(); i++){
        QString aa = this->myDataSourceList[i];
        bool isExist = true;
        for(int j=0; j<resultSourceList.length(); j++){
            QString bb = resultSourceList[j];
            if(aa == bb){
                isExist = false;
            }
        }
        if(isExist)
            studentModel2->setItem(i, 0, new QStandardItem(aa));
    }
    qDebug()<<studentModel2<<endl;

    ui->tableView->setModel(studentModel2) ;

}
